﻿using Century.Business.CMS;
using Century.Model.CMS;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;

namespace Century.Web.UI.MVC.Helper
{
    public static class HtmlHelperExtensions
    {
        /// <summary>
        /// Returns analytic Codes by positions
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        //public static MvcHtmlString RenderAnalyticScripts(this HtmlHelper helper, CountrySiteScriptPositions position)
        //{
        //    return new MvcHtmlString(new CountrySiteScriptBSO().GetSiteScripts(PersistData.CurrentSite.Path, PersistData.CurrentPage.LocalName, position));
        //}
        static ISiteService siteService = new SiteService();

        public static SitePage CurrentPage(this HtmlHelper helper)
        {
            string pageName = helper.ViewContext.RouteData.Values["pgName"].ToString();
            return siteService.GetPage(pageName, FrameworkToken.UserToken.CurrentSite.Id);
        }

        public static List<SitePage> GetSubPages(this HtmlHelper helper)
        {
            SitePage currentPage = CurrentPage(helper);
            return siteService.GetSubPages(currentPage.Id, currentPage.SiteId);
        }

        public static List<Site> GetSites(this HtmlHelper helper)
        {
            return siteService.GetSites();
        }

        public static Site CurrentSite(this HtmlHelper helper)
        {
            return FrameworkToken.UserToken.CurrentSite;
        }

        public static List<SitePage> BreadCrumb(this HtmlHelper helper)
        {
            SitePage currentPage = CurrentPage(helper);
            List<SitePage> sitePages = siteService.GetSitePages(currentPage.SiteId);
            List<SitePage> result = new List<SitePage>();
            int? parentId = currentPage.ParentId;
            if (parentId.HasValue)
            {

                for (int i = 0; i < 10; i++)
                {
                    SitePage page = sitePages.FirstOrDefault(c => c.Id == parentId);
                    if (page != null)
                    {
                        result.Add(page);
                    }
                    else
                    {
                        break;
                    }
                    if (page.ParentId.HasValue)
                    {
                        parentId = page.ParentId;
                    }
                    else
                    {
                        break;
                    }
                }

            }
            return result;
        }

        private class ScriptBlock : IDisposable
        {
            private const string scriptsKey = "scripts";
            public static List<string> pageScripts
            {
                get
                {
                    if (HttpContext.Current.Items[scriptsKey] == null)
                        HttpContext.Current.Items[scriptsKey] = new List<string>();
                    return (List<string>)HttpContext.Current.Items[scriptsKey];
                }
            }

            WebViewPage webPageBase;

            public ScriptBlock(WebViewPage webPageBase)
            {
                this.webPageBase = webPageBase;
                this.webPageBase.OutputStack.Push(new StringWriter());
            }

            public void Dispose()
            {
                pageScripts.Add(((StringWriter)this.webPageBase.OutputStack.Pop()).ToString());
            }
        }

        public static IDisposable BeginScripts(this HtmlHelper helper)
        {
            return new ScriptBlock((WebViewPage)helper.ViewDataContainer);
        }

        public static MvcHtmlString PageScripts(this HtmlHelper helper)
        {
            return MvcHtmlString.Create(string.Join(Environment.NewLine, ScriptBlock.pageScripts.Select(s => s.ToString())));
        }
    }

}
